package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.SellerService;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import entities.SellerEntity;

public class SellerDealServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public SellerDealServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String date = request.getParameter("date");
		String sellerId = request.getParameter("sellerId");
		//1,获得一个商家list
		//2,遍历商家，分别计算已支付未支付和总额，再加上商家id作为一个json对象放入json数组中
		//3,把json数组输出到客户端
		Map<String,Double> orderItem1 = new HashMap();
		Map<String,Double> orderItem2 = new HashMap();
	//	try {
			//orderItem1 = SellerService.getSellerCount(date,sellerId);
			//orderItem2 = SellerService.getSellerCounted(date,sellerId);
			//System.out.println("@#@#@#@#@#@#@#@#"+result+"@#@#@#@#@#@#@#");
		//} catch (SQLException e) {
			// TODO Auto-generated catch block
		//	e.printStackTrace();
		//}
		Set<Map.Entry<String, Double>> set1 = orderItem1.entrySet();
		JsonObject jObject = new JsonObject();

		  for (Iterator<Map.Entry<String, Double>> it = set1.iterator(); it.hasNext();)
	        {
	            Map.Entry<String, Double> entry = it.next();
	            jObject.addProperty(entry.getKey(), entry.getValue());
	        }
		//out.print(result);
		out.println(jObject.toString());
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
